<template>
	<view>
		<uni-search-bar class="search_bar" bgColor1="#f00000" cancelButton="none" radius="35" 
		v-model="kw"
		@input="getSearchList()"></uni-search-bar>
		<!-- 搜索建议列表 -->
		<view class="suggest-list" v-if="suggestList.length !== 0">
			<view class="suggest-item" v-for="(item,i) in suggestList" :key="i" @click="gotoGoodsDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="record_container" v-else>
			<view class="search_history_title">
				<text>搜索记录</text>
				<uni-icons type="trash" size="17" @click="clearSearchRecord()"></uni-icons>
			</view>
			
			<view class="search_list">
				<uni-tag class="uni_tag" :text="item"  
				@click="gotoGoodsList(item)"
				v-for="(item,i) in historyRecord" :key="i" ></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	import { toast } from '../../utils/common';
	export default {
		data() {
			return {
				historyRecord: [],
				//搜索关键字
				kw:'',
				// 搜索的结果列表
				suggestList: []
			};
		},
		methods:{
			async getSearchList() {
				if(this.kw.length === 0){
					this.suggestList = []
					return
				}
				
				const {data: res} =await uni.$http.get('/api/public/v1/goods/qsearch',{ query: this.kw })
				
				if(res.meta.status !== 200) return toast('请求错误！！')
				this.suggestList = res.message;
			},
			gotoGoodsDetail(id){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + id,
				})
				// 搜索之后保存搜索记录
				this.saveSearchHistory();
			},
			
			gotoGoodsList(id){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + id,
				})
			},
			
			saveSearchHistory(){
				
				const set = new Set(this.historyRecord)
				set.delete(this.kw)
				set.add(this.kw)
				
				this.historyRecord = Array.from(set)
				
				//历史记录持久化存储到本地
				uni.setStorageSync('kw',JSON.stringify(this.historyRecord))
			},
			
			clearSearchRecord(){
				this.historyRecord = []
				uni.removeStorageSync('kw')
			}
		},
		onLoad() {
			this.historyRecord = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
.search_bar{
	background-color: #f00000;
}
	
.record_container{
	padding: 0 5px;
	
	.search_history_title{
		display: flex;                  /* 设置为弹性布局 */
		justify-content: space-between; /* 内容两端对齐 */
		align-items: center;           /* 垂直居中对齐 */
		height: 35px;
		font-size: 12px;
		border-bottom: 1px solid #efefef;
	}
	
	.search_list{
		display: flex;
		flex-wrap: wrap;
		
		.uni_tag {
			margin-top: 10px;
			margin-right: 10px;
			.data-v-1516016e{
				background-color: #efefef;
				border-style: none;
				color: #000000;
				font-size: 14px;
			}
		}
	}
}

.suggest-item{
	display: flex;
	align-items: center;
	justify-content: space-between;
	font-size: 13px;
	padding: 12px 0;
	border-bottom: 1px solid #efefef;
	
	.goods-name{
		overflow: hidden;           /* 隐藏超出部分 */
		text-overflow: ellipsis;    /* 溢出文本用省略号表示 */
		white-space: nowrap;        /* 不换行 */
		padding-left: 5px;
	}
}

</style>
